package com.example.sharding.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.example.sharding.commons.Constants;
import com.example.sharding.commons.ResponseBuilder;
import com.example.sharding.domain.entity.Department;
import com.example.sharding.domain.entity.Menu;
import com.example.sharding.domain.entity.User;
import com.example.sharding.service.DepartmentService;
import com.example.sharding.service.MenuService;
import com.example.sharding.service.UserService;
import com.google.common.collect.ImmutableMap;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 *
 * @author wf
 * @date 2019/11/14
 */
@RestController
@RequestMapping("/test")
public class User2Controller extends ResponseBuilder {

    @Autowired
    private UserService userService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private MenuService menuService;

    @GetMapping("/add")
    public Object  add(){
        for (int m=0; m<100;m++) {
            long deptId = IdWorker.getId();
            //分库分表测试
            Department dept = Department.builder()
                    .deptId(deptId)
                    .createTime(DateTime.now().toString(Constants.YYYY_MM_DD_HH_MM_SS))
                    .id(IdWorker.getId())
                    .name("测试" +String.valueOf(m)).build();


            User build = User.builder().id(IdWorker.getId())
                    .isDelete(1)
                    .name("测试"+String.valueOf(m))
                    .phone(String.valueOf(DateTime.now().getMillis()).substring(0, 11))
                    .deptId(deptId)
                    .createTime(DateTime.now().toString(Constants.YYYY_MM_DD_HH_MM_SS)).build();

            Menu menu = Menu.builder().id(IdWorker.getId())
                    .createTime(DateTime.now().toString(Constants.YYYY_MM_DD_HH_MM_SS))
                    .isDelete(1)
                    .name("测试" + String.valueOf(m))
                    .build();
            departmentService.save(dept);
            userService.save(build);
            menuService.save(menu);
        }
        return success();
    }


    @GetMapping("/select")
    public Object  list(){
        QueryWrapper<Department> departmentQueryWrapper = new QueryWrapper<>();
        departmentQueryWrapper.likeRight("name","测试5").eq("id","");
        List<Department> departmentList= departmentService.listByParam("测试5");
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.likeRight(true,User::getName,"测试2").eq(User::getIsDelete,1);  //非空判断写法
        List<User> userList = userService.list(lambdaQueryWrapper);
        List<Menu> menuList = menuService.list(new QueryWrapper<Menu>().lambda().eq(Menu::getName, "测试100"));
        return  success(ImmutableMap.of("departmentList",departmentList,"userList",userList,"menuList",menuList));
    }

    //多表联查
    @GetMapping("/getDeptByUserId")
    public Object  getDeptByUserId(@RequestParam("userId") String userId){

        return  success(userService.getDeptByUserId(userId));
    }


}
